import React,{ Component } from 'react';
import { TreeSelect } from 'antd';

//let data1111;
const personArr = [
    {
        key: "001",
        name: "maimang",
        children: [
            {
                key: "002",
                name: "Tom",
                headImg: "https://avatars1.githubusercontent.com/u/24405319?s=460&v=4"
            },
            {
                key: "003",
                name: "Jerry",
                headImg: "https://avatars1.githubusercontent.com/u/24405319?s=460&v=4"
            },
            {
                key: "006",
                name: "业务部",
                children: [
                    {
                        key: "007",
                        name: "小明",
                        headImg:
                            "https://avatars1.githubusercontent.com/u/24405319?s=460&v=4"
                    },
                    {
                        key: "008",
                        name: "小话",
                        headImg:
                            "https://avatars1.githubusercontent.com/u/24405319?s=460&v=4"
                    },
                    {
                        key: "009",
                        name: "小王",
                        headImg:
                            "https://avatars1.githubusercontent.com/u/24405319?s=460&v=4"
                    }
                ]
            },
            {
                id: "010",
                name: "test",
                headImg: "https://avatars1.githubusercontent.com/u/24405319?s=460&v=4"
            }
        ]
    }
];
//var result=LsCurrentPart('007',personArr);
//console.log(result);

function getData() {
    let url = 'https://cs-gateway.oppein.com/usercenter/enterprise/op_1000/group2?feature=account_center&token=b3BfMTAwMF8xYzgwMGNmYi0wOGQ2LTQ1MWItOWJhZi03ZTk3N2M4OGZhZTA=';
    fetch(url,{
        method:'GET',
        headers: {
            'Content-Type': 'application/json'
        },
    }).then(res => {
        return res.json();
    }).then(data => {
        if(data.code == 200) {
            let treeData = data.data.group;
            console.log('getData',treeData);
            let res = LsCurrentPart("g-8a64c2a6-93a7-48ec-88fc-617d729d8b20",treeData);
            console.log(res);
        }
    }).catch(err => {
        console.log(err);
    });
}
getData();
let result;
function LsCurrentPart(id, items) {
    for (var i in items) {
        let item = items[i];
        if (item.groupid == id) {
            result = item;
            break;
        } else if (item.children && item.children.length>0) {
            result = LsCurrentPart(id, item.children);
        }
    }
    return result;
}

class TestTreeSelect8 extends Component {
    constructor(props){
        super(props);
        this.state = {
            value: "op_1000_9999",
            treeData: [],
        }
    }
    componentDidMount() {
        let url = 'https://cs-gateway.oppein.com/usercenter/enterprise/op_1000/group2?feature=account_center&token=b3BfMTAwMF8xYzgwMGNmYi0wOGQ2LTQ1MWItOWJhZi03ZTk3N2M4OGZhZTA=';
        fetch(url,{
            method:'GET',
            headers: {
                'Content-Type': 'application/json'
            },
        }).then(res => {
            return res.json();
        }).then(data => {
            if(data.code == 200) {
                let treeData = data.data.group;

                let result = this.dealData(treeData);
                console.log('treeData',treeData);
                let res = this.aa(result);
                console.log('res',res);

                let res1 = this.getCurrentPart("g-2da161d6-b8d6-4f25-878a-6595b7eb6314",treeData);
                console.log('res1',res1);

                this.setState({
                    treeData:result,
                })
            }
        }).catch(err => {
            console.log(err);
        });
    }

    dealData(treeData) {
        let newItem;
        let newArr = treeData.map((item,index)=>{
            newItem = {};

            if(item.children) {
                let arr = this.dealData(item.children);
                newItem.children = arr;
            }
            newItem.title = item.groupname;
            newItem.value =item.groupid;
            newItem.key = item.groupid;

            // newItem = {
            //     title: item.groupname,
            //     value: item.groupid,
            //     key: item.groupid,
            // }
            return newItem;
        })
        return newArr;
    }

    aa(treeData) {
        let data1111;
        function dealData1(treeData) {
            treeData.find((item,index)=>{
                if (item.key == 'g-a9b9a1ab-9c5f-4322-93d2-3e09f2bde89d') {
                    data1111 = item;
                }
                if (!data1111 && item.children && item.children.length > 0) {
                    dealData1(item.children);
                }
            });
        }
        dealData1(treeData);
        return data1111;
    }
    dealData2(key,data) {
        let result;
        for (var i in data) {
            let item = data[i];
            if (item.key == key) {
                result = item;
                break;
            } else if (item.children) {
                result = this.dealData2(key,item.children);
            }
        }
        return result;
    }

    getCurrentPart(id, items) {
        let result;
        // for (var i in items) {
        //     let item = items[i];
        //     if (item.key == id) {
        //         result = item;
        //         break;
        //     } else if (item.children && item.children.length > 0) {
        //         result = this.getCurrentPart(id, item.children);
        //     }
        // }

        items.forEach(item=>{
            if(item.groupid == id) {
                result = item;
            }else if(item.children > 0){
                this.getCurrentPart(id,item.children);
            }
        })
        console.log('result123',result);
        return result;
    }

    getFun(id, items) {
        let result;
        for (var i in items) {
            let item = items[i];
            if (item.key == id) {
                result = item;
                break;
            } else if (item.children && item.children.length>0) {
                result = this.getFun(id, item.children);
            }
        }
        return result;
    }
    onChange = (value) => {
        console.log(value);
        this.setState({ value });
    }
    render() {
        var treeData = [];
        var data = [{
            key: "1",
            name: "根节点",
            children:[
                {
                    key: "2",
                    name: "节点1",
                    children: [
                        {
                            key: "3",
                            name: "节点3",
                        },{
                            key: "4",
                            name: "节点4"
                        }
                    ]
                }
            ]
        }]
        return (
            <div>
                <TreeSelect
                    style={{ width: 300 }}
                    value={this.state.value}
                    dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
                    treeData={ this.state.treeData}
                    placeholder="请选择组"
                    treeDefaultExpandAll
                    onChange={this.onChange}
                />
                {
                    //console.log('getFun',this.getFun("3",data))
                }
            </div>
        )
    }
}
export default TestTreeSelect8;